package com.vcar.LDiceClient.core
{
	import com.vcar.LDiceClient.config.Config;
	import com.vcar.LDiceClient.config.GlobalEventName;
	import com.vcar.LDiceClient.config.MediatorName;
	
	import flash.display.DisplayObject;
	import flash.system.ApplicationDomain;
	
	import mx.core.Container;
	import mx.core.UIComponent;
	
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * 弹出窗口管理类
	 * @author rhj
	 * <p>
	 * 此类为popupmanager的应用接口类，常用方法将整合到facade中<br/>
	 * 用法：<br/>
	 * 弹出module
	 * ModWindowManager.addModulePopUp("module url")<br/>
	 * 弹出普通窗口
	 * ModWindowManager.addComponentPopUp(view);
	 * </p>
	 */
	public class ModWindowManager extends Mediator
	{
		public static const REEL_TYPE:int = 1;
		public static const NORMAL_TYPE:int = 2;
		private static var winstack:Array; //当前已当开的弹出窗口，后打开的在后面
		
		private static var _instance:ModWindowManager;
		
		private var applicationDomain:ApplicationDomain;
		
		private var winNameList:Array;
		
		public function ModWindowManager()
		{
			if(_instance==null)
			{
				super(MediatorName.ModWindowManager,null);	
			}
			else
			{
				throw new Error("单件模式不能实例化");	
			}
			
			facade.registerMediator(this);
			
			winstack=new Array();
		}
		
		/**
		 * 监听系统的快捷键
		 * @return
		 * 处理esc键关闭当前的窗口
		 */        
		override public function listNotificationInterests():Array
		{
			return [GlobalEventName.STAGE_HOTKEY_PRESS];
		}
		
		public static function getInstance():ModWindowManager
		{
			if(_instance==null)
			{
				_instance=new ModWindowManager();
			}
			return _instance;
		}
		
		public function escClick(msg:String,body:*):void
		{
			if(winstack.length>0 )
			{
				removePopUp(winstack[winstack.length-1] as UIComponent);
			}
		}
		
		public function init(_container:Container):void
		{
			applicationDomain=ApplicationDomain.currentDomain;
			PopUpManager.initialize(_container);
		}
		
		public function showAlert(msg:Object,modal:Boolean=false):void
		{
			var alert:Alert = new Alert();
			alert.dataObject = msg;
			winstack.push(alert);
			PopUpManager.addPopUp(alert,modal);
		}
		
		public function getWindowsNum():int
		{
			return winstack.length;
		}
		
		/**
		 * 打开外部通过url加载进来的mudule窗口
		 * @param url 当前swf的相对路径
		 * @param data
		 * @param modal
		 * @param type
		 * @param width
		 * @param height
		 * @param center
		 * @return
		 * @see moduleName
		 */
		public function addModulePopUp(url:String,data:Object=null,modal:Boolean=true,type:int=1,width:Number=-1,height:Number=-1,center:Boolean=true):ModWindow
		{
			if(url == 'Alert'){
				showAlert(data,modal);
				return null;
			}else{
				url=url+"?v="+Config.ver;
				
				var mod:ModWindow=new ModWindow(url,data,applicationDomain,type,width,height,center);						
				winstack.push(mod);
				PopUpManager.addPopUp(mod,modal);
				
				return mod;	
			}								
		}
		
		/**
		 * 打开普通的弹出窗口
		 * @deprecated
		 * @param view 打开的弹出窗口实例
		 * @param modal true 模式窗口
		 *
		 */        
		public function addComponentPopUp(view:UIComponent,modal:Boolean=false,center:Boolean=false):void
		{
			winstack.push(view);
			
			PopUpManager.addPopUp(view,modal);
			if(center)
			{
				PopUpManager.centerPopUp(view);
			}
			
		}
		
		public function bringToFront(win:DisplayObject):void
		{
			PopUpManager.bringToFront(win);
		}
		
		public function contains(win:DisplayObject):Boolean
		{
			return PopUpManager.contains(win);
		}
		
		public function removePopUp(mod:UIComponent,animation:Boolean=true):void
		{
			for(var i:int=winstack.length-1;i>-1;i--){
				if(winstack[i] == mod){
					winstack.splice(i,1);
					break;
				}
			}
			
			if(PopUpManager.contains(mod))
				PopUpManager.removePopUp(mod);
			
			if(winstack.length>0)
			{
				(winstack[winstack.length-1] as UIComponent).setFocus();
			}
		}
		
		public function closeAll():void
		{
			winNameList = [];
			winstack = [];
			PopUpManager.removeAllPopUp();
		}
	}
}

